package com.may.wind.controller;

import com.may.wind.entity.Article;
import com.may.wind.pojo.Result;
import com.may.wind.pojo.WindPage;
import com.may.wind.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 风
 */
@RestController
@Api(tags = "文章相关API")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    /**
     * 添加或更新下文章
     *
     * @param article 文章
     * @return 操作结果
     */
    @ApiOperation(value = "添加或更新下文章", httpMethod = "POST")
    @ApiImplicitParam(name = "article", value = "文章", required = true)
    @PostMapping("api/article/saveArticle")
    public Result saveArticle(@Valid Article article) {
        articleService.addOrUpdate(article);
        return Result.ok().data("message", "保存成功");
    }

    /**
     * 获取文章列表
     *
     * @param size 行数
     * @param page 页数
     * @return 文章列表
     */
    @ApiOperation(value = "获取文章列表", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "行数", required = true, paramType = "path", dataType = "int"), @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "path", dataType = "int")})
    @GetMapping("/api/article/{size}/{page}")
    public Result getArticles(@PathVariable("size") int size, @PathVariable("page") int page) {
        WindPage articles = articleService.list(page - 1, size);
        return Result.ok().data("articles", articles);
    }

    /**
     * 查询文章详细内容
     *
     * @param id 文章ID
     * @return 文章详细内容
     */
    @ApiOperation(value = "查询文章详细内容", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, paramType = "path", dataType = "int")
    @GetMapping("/api/article/{id}")
    public Result getArticle(@PathVariable("id") int id) {
        Article article = articleService.findById(id);
        return Result.ok().data("article", article);
    }

    /**
     * 删除文章
     *
     * @param id 文章ID
     * @return 操作结果
     */
    @ApiOperation(value = "删除文章", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, paramType = "path", dataType = "int")
    @DeleteMapping("/api/article/delete/{id}")
    public Result deleteArticle(@PathVariable("id") int id) {
        articleService.delete(id);
        return Result.ok().data("message", "删除成功");
    }
}
